package com.zbzly.mcp.controller;

import com.zbzly.mcp.tool.TimeTool;
import jakarta.annotation.Resource;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.model.function.FunctionCallback;
import org.springframework.ai.ollama.OllamaChatModel;
import org.springframework.ai.tool.ToolCallbackProvider;
import org.springframework.ai.tool.function.FunctionToolCallback;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

import java.util.List;

@RestController
public class MCPController {
    @Resource
    private List<ToolCallbackProvider> toolCallbackProviderList;
    @Resource
    private OllamaChatModel ollamaChatModel;

    @GetMapping(value = "/mcp/hello",produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    public Flux<String> hello(String question) {

        return ChatClient.builder(ollamaChatModel)
                .build()
                .mutate()
                .defaultTools(FunctionToolCallback
                        .builder("TimeTool", new TimeTool())
                        .description("获取当前时间 例如：2025-04-05T21:28:05.945204")
                        .inputType(Void.class)
                        .build())
                .defaultTools(toolCallbackProviderList)
                .build()
                .prompt(question)
                .stream()
                .content();
    }
}
